package com.junlong0716.enjoylearning.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.HomeActivity
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.constant.NetConstant.REQUEST_HEADER
import com.junlong0716.enjoylearning.dialog.LoadingDialog
import com.junlong0716.enjoylearning.entity.AccountEntity
import com.junlong0716.enjoylearning.manager.UserManager
import com.junlong0716.enjoylearning.net.api.NetApi.API_LOG_IN
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import kotlinx.android.synthetic.main.activity_log_in.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject

class LogInActivity : RxAppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_log_in)
        view_divider.visibility = View.GONE



        bt_log_in.setOnClickListener {

            //            if (!RegexUtils.isMobileExact(det_phone.text.toString().trim())) {
//                ToastUtils.showShort("手机号不合法！")
//                return@setOnClickListener
//            }

            if (det_pwd.text.toString().trim().length < 6) {
                ToastUtils.showShort("密码长度不合法！")
                return@setOnClickListener
            }
            val loadingDialog = LoadingDialog(this@LogInActivity)
            loadingDialog.setCanceledOnTouchOutside(false)
            loadingDialog.show()
            val map = HashMap<String, String>()
            map["account"] = det_phone.text.toString().trim()
            map["pwd"] = det_pwd.text.toString().trim()
            val jsonObj = JSONObject(map)
            OkGo.post<String>(REQUEST_HEADER + API_LOG_IN)
                    .tag(this)
                    .upJson(jsonObj)
                    .execute(object : StringCallback() {
                        override fun onSuccess(response: Response<String>?) {
                            loadingDialog.dismiss()
                            val jsonObject = JSONObject(response!!.body())
                            if (jsonObject.getInt("code") == 200) {
                                val token = jsonObject.getString("data")
                                val userInfo = jsonObject.getJSONObject("dataMap").getJSONObject("user")
                                val headImg = userInfo.getString("headImg")
                                val userName = userInfo.getString("userName")
                                val userId = userInfo.getInt("id")
                                val userPhone = userInfo.getString("userPhone")
                                val userAccount = userInfo.getString("userAccount")
                                MyApp.instance().putUserAccount(AccountEntity(userId, userAccount, token, headImg, userName, userPhone))
                                UserManager.setLoginState(true)
                                startActivity(Intent(this@LogInActivity, HomeActivity::class.java))
                                finish()
                            } else {
                                ToastUtils.showShort("账号或密码错误！")
                            }
                        }

                        override fun onError(response: Response<String>?) {
                            super.onError(response)
                            ToastUtils.showShort("请求失败！")
                            loadingDialog.dismiss()
                        }
                    })
        }
    }
}
